package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.exce.SubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Pattern;

@Slf4j
public class SubjectExcelListenerxtends extends AnalysisEventListener<SubjectData> {
    //用于添加课程操作
    public EduSubjectService eduSubjectService;

    public SubjectExcelListenerxtends(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }
    public SubjectExcelListenerxtends( ) {

    }

    //监听器
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData ==  null){
            throw new GuliException(20001,"文件数据为空");
        }
        //一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类
        EduSubject oneSubjectName = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
        //添加一个对象
        if (oneSubjectName == null){
            oneSubjectName = new EduSubject();
            oneSubjectName.setParentId("0");
            oneSubjectName.setTitle(subjectData.getOneSubjectName());
            System.out.println("一级分类id："+oneSubjectName);
            eduSubjectService.save(oneSubjectName);
            System.out.println("一级分类id："+oneSubjectName);

        }
        String pid = oneSubjectName.getId();

        EduSubject twoOneSubjectName = existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), pid);

        if (twoOneSubjectName == null){
            twoOneSubjectName = new EduSubject();
            twoOneSubjectName.setParentId(pid);
            twoOneSubjectName.setTitle(subjectData.getTwoSubjectName());
            eduSubjectService.save(twoOneSubjectName);
        }
    }
    //判断一级分类不能重复添加
    private EduSubject existOneSubject(EduSubjectService eduSubjectService,String name){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        //eq是SQL中的=操作符，即等于
//        if( Pattern.compile("[^a-zA-Z]").matcher(name).find()){
//            wrapper.eq("LOWER(title)","name.toLowerCase()");
//        }
        wrapper.eq("title",name);

        wrapper.eq("parent_id","0");

        //getOne方法会从数据库中获取满足条件的第一条记录。如果表中有多个匹配的记录，只返回其中的一个。
        return eduSubjectService.getOne(wrapper);
    }

    private EduSubject existTwoSubject(EduSubjectService eduSubjectService,String name,String pid){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        //eq是SQL中的=操作符，即等于
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        //getOne方法会从数据库中获取满足条件的第一条记录。如果表中有多个匹配的记录，只返回其中的一个。
        return eduSubjectService.getOne(wrapper);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
